<template>
  <div class="apartment">
    <div class="apart-serach">
      <div class="header-fixed"><check-bar @brandFilter="getFilterData"></check-bar></div>
    </div>
    <div class="apart-list" v-for="(item, index) in apartmentList" :key="index" v-if="apartmentList.length > 0">
      <div class="apart-list-item" @click="goDetail(item.id)">
        <div class="item-img">
          <img :src="item.houseCoverUrl ? item.houseCoverUrl : ''" alt="">
        </div>
        <div class="item-desc">
          <!-- <p class="m-title">{{sourceName[item.source]}} {{item.projectNmae}}</p> -->
          <p class="m-title">{{item.name}}</p>
          <!-- <p class="m-title">{{item.projectNmae}}~{{item.name}}</p> -->
          <p class="m-address">地址：{{item.detailAddress}}</p>
          <p><span class="m-price">￥{{item.minPrice}}</span>元/月</p>
          <p v-if="item.houseCharacteristic">
            <span class="m-tag-item" v-for="(item, index) in item.houseCharacteristic.split(',')" :key="index">{{item}}</span>
          </p>
        </div>
        <div class="item-logo" v-if="item.isFreeDeposit == 1">诚信免押</div>
        <img class="renting-logo-img" src="../../images/renting-logo.png" v-if= "item.status === 1002">
      </div>
    </div>
    <div class="load-more" v-show="moreData" @click="loadMore">加载更多>></div>
    <div class="load-more" v-show="noCheckData">没有符合筛选条件数据</div>
    <div class="load-more" v-show="noMoreData">没有更多数据了</div>
  </div>
  
</template>
<script>
import { Cell } from 'vux'
import checkBar from './checkBar'
import noData from '@/components/noData'
export default {
  data () {
    return {
      apartmentList: [],
      noCheckData: false, // 状态
      brandFilterData: {}, // 筛选数据
      moreData: false, // 显示更多
      noMoreData: false, // 暂无更多数据
      page: 1, // 总数
      pageSize: 10, // 总数
      pageCount: 0 // 总页数
    }
  },
  created () {
    let brandFilterData = JSON.parse(window.localStorage.getItem('brandFilterData') || '{}')
    this.getHouseApartmentList(brandFilterData)
  },
  components: {
    Cell,
    checkBar,
    noData
  },
  methods: {
    getFilterData (brandFilterData = {}) {
      console.log(brandFilterData)
      this.apartmentList = []
      this.page = 1
      this.moreData = false
      this.noCheckData = false
      this.noMoreData = false
      this.brandFilterData = brandFilterData
      this.getHouseApartmentList(brandFilterData)
    },
    getHouseApartmentList: function (brandFilterData = {}, isMore = 0) {
      let {region, rentalPriceValue, freeDeposit, brand} = brandFilterData
      let minPrice = ''
      let maxPrice = ''
      if (rentalPriceValue) {
        rentalPriceValue = rentalPriceValue.replace(/[\u4e00-\u9fa5]/g, '')
        if (rentalPriceValue.indexOf('-') !== -1) {
          minPrice = rentalPriceValue.split('-')[0] || ''
          maxPrice = rentalPriceValue.split('-')[1] || ''
        } else {
          if (+rentalPriceValue > 1000) {
            minPrice = rentalPriceValue
          } else {
            maxPrice = rentalPriceValue
          }
        }
      }
      let postData = {
        regionNo: region || '',
        minPrice,
        maxPrice,
        depositMethod: freeDeposit || '',
        ascription: brand || '',
        page: this.page,
        pageSize: this.pageSize
      }
      this.$axiosPosting(this.$api.getApartmentHouseList, postData).then(res => {
        if (res.code === '0000') {
          let newList = res.list || []
          if (!isMore) {
            if (!newList.length) {
              this.noCheckData = true
              this.$vux.toast.text(`暂无数据`)
            } else {
              if (res.count) this.$vux.toast.text(`共找到${res.count}套房源`)
            }
          }
          newList.forEach((item) => {
            this.apartmentList.push(item)
          })
          this.pageCount = Math.ceil(res.count / this.pageSize)
          if (this.page < this.pageCount) {
            this.moreData = true
          }
        }
      })
    },
    goDetail: function (id) {
      this.$router.push({path: '/apartmentDetail', query: { id: id }})
    },
    loadMore () {
      this.page ++
      if (this.page > this.pageCount) {
        this.noMoreData = true
        this.moreData = false
      } else {
        this.getHouseApartmentList(this.brandFilterData, 1)
      }
    }
  },
  beforeDestroy () {
    this.$vux.toast.hide()
  }
}
</script>
<style lang="less">
  .apartment{
    font-size: 12px;
    color:#979797;
    min-height: 100vh;
    box-sizing: border-box;
    background: #fff;
    -webkit-overflow-scrolling: touch;
    .load-more{
      font-size: 15px;
      text-align: center;
      color:#ff9600;
      height: 50px;line-height: 50px;
      background: #fff;
      border-top: 1px solid #eee;
    }
    .apart-serach{
      background: #fff!important;
      height:50px;
      .header-fixed{
        background: #fff;
        // z-index: 1000;
        height:50px;
      }
    }
    .apart-list{
      border-top: 1px solid #eee;
      padding: 18px 0;
      &:first-child{
        border: none;
      }
      line-height: 1.3;
      .apart-list-item{
        // padding: 18px 0px;
        // padding-bottom: 18px;
        position: relative;
        overflow: hidden;
        background: #fff;
        .item-img{
          width: 100%;
          height: 8.5rem;
          overflow: hidden;
          object-fit: cover;
          img{
            width: 100%;
            object-fit: cover;
            height: 100%;
          }
        }
        .item-desc{
          p{
            padding-bottom: 8px;
            &:nth-last-child(1),&:nth-last-child(2){
              padding-bottom: 4px;
            }
          }
          padding: 15px;
          padding-bottom: 0px;
          display: flex;
          flex-direction:column;
          justify-content: space-between;
          .m-title{
            color:#262626;
            font-size: 17px;
            text-overflow: ellipsis;
            overflow: hidden;
            white-space: nowrap;
            padding-bottom: 7px;
          }
          .m-address{
            text-overflow: ellipsis;
            overflow: hidden;
            white-space: nowrap;
            padding-bottom: 10px;
          }
          .m-price{
            font-weight: bold;
            font-size: 15px;
            color:#ff3c00;
            font-style: normal;
            font-family: Microsoft YaHei, Tahoma, Arial, sans-serif;
          }
          .m-tag-item{
            border-radius: 3px;
            border:1px solid #ff9600;
            color:#ff9600;
            display: inline-block;
            padding:2px 10px;
            margin-right: 7px;
            margin-top: 8px;
          }
        }
        .renting-logo-img{
          width: 6rem;
          position: absolute;
          right:0;
          top:0;
        }
        .item-logo{
          position: absolute;
          top: 0.9rem;
          width: 2.95rem;
          height: 1.1rem;
          background: #fc9400;
          font-size: 0.7rem;
          text-align: center;
          color: #fff;
          line-height: 1.1rem;
        }
      }
    }
  }
</style>
